using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.ConnectionEngine.Registers.Health.Patient.Common
{
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://Microsoft.ConnectionEngine/Schemas/PatientRegister")]
    public class Patients
    {
        private List<Patient> patientList;

        public List<Patient> Items
        {
            get { return patientList; }
            set { patientList = value; }
        }

        public Patients()
        {
            patientList = new List<Patient>();
        }

        public Patients(List<Patient> newPatientList)
        {
            patientList = newPatientList;
        }
    }



    public class Person
    {
        private Names names;
        public Names Names
        {
            get
            {
                if (names == null)
                    names = new Names();
                return names;
            }
            set
            {
                //validate there's one, and only one, preferred name
                Names listNames = value;
                int iNoOfPreferredNames = 0;
                foreach (Name name in listNames)
                {
                    if (name.PrimaryAlias)
                        iNoOfPreferredNames++;
                }
                if (iNoOfPreferredNames > 1)
                    throw new Exception("You can not have more than one Primary Name");
                if (iNoOfPreferredNames < 0)
                    throw new Exception("You must have at least one Primary Name");

                names = listNames;
            }
        }


        public string PerferredFamilyName
        {
            get
            {
                string returnName = "";
                if (names != null)
                {
                    foreach (Name subName in names)
                    {
                        if (subName.PrimaryAlias)
                        {
                            returnName = subName.FamilyName;
                            break;
                        }
                    }
                }
                return returnName;
            }
        }

        public string PerferredGivenName
        {
            get
            {
                string returnName = "";
                if (names != null)
                {
                    foreach (Name subName in names)
                    {
                        if (subName.PrimaryAlias)
                        {
                            switch (subName.PreferredGivenNameIndex)
                            {
                                case 1: returnName = subName.GivenName1;
                                    break;
                                case 2: returnName = subName.GivenName2;
                                    break;
                                case 3: returnName = subName.GivenName3;
                                    break;
                                default: returnName = subName.GivenName1;
                                    break;
                            }
                            break;
                        }
                    }
                }
                return returnName;
            }
        }

        DateTime dateOfBirth;
        public DateTime DateOfBirth
        {
            get { return dateOfBirth; }
            set { dateOfBirth = value; }
        }

        DateTime dateOfDeath;
        public DateTime DateOfDeath
        {
            get { return dateOfDeath; }
            set { dateOfDeath = value; }
        }

        string gender;
        public string Gender
        {
            get { return gender; }
            set { gender = value; }
        }

    }
    
    
    //JH21122005[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://Microsoft.ConnectionEngine/Schemas/PatientRegister")]
    public class Patient : Person
    {
        string patientID;
        public string PatientID
        {
            get { return patientID; }
            set { patientID = value; }
        }

        string addressLine1;
        public string AddressLine1
        {
            get { return addressLine1; }
            set { addressLine1 = value; }
        }

        string addressLine2;
        public string AddressLine2
        {
            get { return addressLine2; }
            set { addressLine2 = value; }
        }

        string addressSuburb;
        public string AddressSuburb
        {
            get { return addressSuburb; }
            set { addressSuburb = value; }
        }
        
        string addressTownCity;
        public string AddressTownCity
        {
            get { return addressTownCity; }
            set { addressTownCity = value; }
        }

        string addressCountry;
        public string AddressCountry
        {
            get { return addressCountry; }
            set { addressCountry = value; }
        }

        string addressCode;
        public string AddressCode
        {
            get { return addressCode; }
            set { addressCode = value; }
        }
        bool donorFlag;
        public bool DonorFlag
        {
            get { return donorFlag; }
            set { donorFlag = value; }
        }
        
        public Patient()
        { }

        public Patient(Common.DataSets.dsPatient dsPat, int index)
        {
            this.PatientID = dsPat.Patient[index].PatientID;
            this.AddressCode = dsPat.Patient[index].AddressCode;
            this.AddressCountry = dsPat.Patient[index].AddressCountry;
            this.AddressLine1 = dsPat.Patient[index].AddressLine1;
            this.AddressLine2 = dsPat.Patient[index].AddressLine2;
            this.AddressSuburb = dsPat.Patient[index].AddressSuburb;
            this.AddressTownCity = dsPat.Patient[index].AddressTownCity;
            this.DateOfBirth = dsPat.Patient[index].DateOfBirth;
            this.DateOfDeath = dsPat.Patient[index].DateOfDeath;
            this.DonorFlag = dsPat.Patient[index].donorFlag;
            this.Gender = dsPat.Patient[index].Gender;
            this.Names = new Names();

            Name name;

            foreach (Common.DataSets.dsPatient.NamesRow nRow in dsPat.Patient[index].GetChildRows("Patient_Names"))
            {
                name = new Name(nRow.Title,
                                nRow.FamilyName,
                                nRow.GivenName1,
                                nRow.GivenName2,
                                nRow.GivenName3,
                                nRow.PreferredGivenNameIndex,
                                nRow.PrimaryAliasIndicator);
                this.Names.Add(name);
            }
        }

        /// <summary>
        /// Check that the patient holds valid information. Will throw an exception if it is not.
        /// </summary>
        public void ValidatePatient()
        {
            int iNoOfPreferredNames = 0;
            foreach (Name name in this.Names)
            {
                if (name.PrimaryAlias)
                    iNoOfPreferredNames++;
            }
            if (iNoOfPreferredNames > 1)
                throw new Exception("You can not have more than one Primary Name");
            if (iNoOfPreferredNames < 0)
                throw new Exception("You must have at least one Primary Name");

        }

    }
}
